﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="User.cs" company="DissingNelleman">
//   
// All rights reserved. </copyright>
// <summary>
//   The user.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceLibrary.Services.Dto
{
    using System;
    using System.Runtime.Serialization;

    /// <summary>
    /// The user.
    /// </summary>
    [DataContract]
    public class User
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the email.
        /// </summary>
        [DataMember]
        public string Email { get; set; }

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        [DataMember]
        public Guid ID { get; set; }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        [DataMember]
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the phone number.
        /// </summary>
        [DataMember]
        public string PhoneNumber { get; set; }

        /// <summary>
        /// Gets or sets the username.
        /// </summary>
        [DataMember]
        public string Username { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The convert.
        /// </summary>
        /// <param name="user">
        /// The user.
        /// </param>
        /// <returns>
        /// The <see cref="User"/>.
        /// </returns>
        public static User Convert(Models.Domain.User user)
        {
            if (user != null)
            {
                var dto = new User
                              {
                                  ID = user.ID, 
                                  Email = user.Email, 
                                  Name = user.Name, 
                                  PhoneNumber = user.PhoneNumber, 
                                  Username = user.Username
                              };

                return dto;
            }
            
            return null;
        }

        #endregion
    }
}

namespace ServiceLibrary.Services.Queries
{
    using System.Runtime.Serialization;

    using NHibernate;

    using ServiceLibrary.Actioners.Base;
    using ServiceLibrary.Services.Results;

    /// <summary>
    /// The users query.
    /// </summary>
    [DataContract]
    public class UsersQuery : IQuery
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the mail_ is_ like.
        /// </summary>
        [DataMember]
        public string MailIsLike { get; set; }

        /// <summary>
        /// Gets or sets the name_ is_ like.
        /// </summary>
        [DataMember]
        public string NameIsLike { get; set; }

        /// <summary>
        /// Gets or sets the username_ equals.
        /// </summary>
        [DataMember]
        public string UsernameEquals { get; set; }

        [DataMember]
        public bool? OnlyActiveUsers { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <returns>
        /// The <see cref="IResult"/>.
        /// </returns>
        public override IResult Execute(ISession hsession)
        {
            var actioner = new UserActioner(hsession, null);
            return actioner.ExecuteSearchQuery(this);
        }

        #endregion
    }

    /// <summary>
    /// The validate user query.
    /// </summary>
    [DataContract]
    public class ValidateUserQuery : IQuery
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the password.
        /// </summary>
        [DataMember]
        public string Password { get; set; }

        /// <summary>
        /// Gets or sets the username.
        /// </summary>
        [DataMember]
        public string Username { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <returns>
        /// The <see cref="IResult"/>.
        /// </returns>
        public override IResult Execute(ISession hsession)
        {
            var actioner = new UserActioner(hsession, null);
            return actioner.ValidateLogin(this);
        }

        #endregion
    }
}

namespace ServiceLibrary.Services.Results
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.Serialization;

    using ServiceLibrary.Services.Dto;

    /// <summary>
    /// The users result.
    /// </summary>
    [DataContract]
    public class UsersResult : IResult
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="UsersResult"/> class.
        /// </summary>
        public UsersResult()
        {
            this.List = new List<User>();
            this.TotalRowCount = 0;
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the list.
        /// </summary>
        [DataMember]
        public List<User> List { get; set; }

        /// <summary>
        /// Gets or sets the total row count.
        /// </summary>
        [DataMember]
        public int TotalRowCount { get; set; }

        #endregion
    }

    /// <summary>
    /// The validate user result.
    /// </summary>
    [DataContract]
    public class ValidateUserResult : IResult
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the user id.
        /// </summary>
        [DataMember]
        public Guid UserID { get; set; }

        #endregion
    }
}

namespace ServiceLibrary.Services.Commands
{
    using System;
    using System.Runtime.Serialization;

    using NHibernate;

    using ServiceLibrary.Actioners.Base;
    using ServiceLibrary.Services.Dto;

    /// <summary>
    /// The create user command.
    /// </summary>
    [DataContract]
    public class CreateUserCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the encrypted password.
        /// </summary>
        [DataMember]
        public string EncryptedPassword { get; set; }

        /// <summary>
        /// Gets or sets the user.
        /// </summary>
        [DataMember]
        public User User { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, Models.Domain.User activeUser)
        {
            var actioner = new UserActioner(hsession, activeUser);
            actioner.CreateUser(this);
        }

        #endregion
    }

    /// <summary>
    /// The update user command.
    /// </summary>
    [DataContract]
    public class UpdateUserCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the updated user.
        /// </summary>
        [DataMember]
        public User UpdatedUser { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, Models.Domain.User activeUser)
        {
            var actioner = new UserActioner(hsession, activeUser);
            actioner.UpdateUser(this);
        }

        #endregion
    }

    /// <summary>
    /// The update user password command.
    /// </summary>
    [DataContract]
    public class UpdateUserPasswordCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        [DataMember]
        public Guid ID { get; set; }

        /// <summary>
        /// Gets or sets the new password.
        /// </summary>
        [DataMember]
        public string NewPassword { get; set; }

        /// <summary>
        /// Gets or sets the old password.
        /// </summary>
        [DataMember]
        public string OldPassword { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, Models.Domain.User activeUser)
        {
            var actioner = new UserActioner(hsession, activeUser);
            actioner.UpdatePassword(this);
        }

        #endregion
    }

    /// <summary>
    /// The update user password command.
    /// </summary>
    [DataContract]
    public class DeactivateUserCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        [DataMember]
        public Guid ID { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, Models.Domain.User activeUser)
        {
            var actioner = new UserActioner(hsession, activeUser);
            actioner.DeactiveUser(this);
        }

        #endregion
    }
}